home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.07 Jul 88 / Window Sample Stuff / FontMap.Asm < prev    next >
Encoding:
Assembly Source File  |  1987-05-06  |  9.4 KB  |  306 lines  |  [TEXT/EDIT]

  1. ;---------------------------- SetupFontMap -------------------------------
  2.  
  3. ; Written by Ray.A.Cameron
  4. ; Version 1.0
  5. ; Mon May 4, 1987 21:41:50
  6.  
  7.  
  8. XDef            SetupFontMap        ; Routine's name
  9. XDef            FontMap            ; Global location for the FontMap's Handle
  10.  
  11.  
  12. ;---------- Includes -------------
  13.  
  14. Include         Traps.D            ; Use System and ToolBox traps
  15. Include            PackMacs.Txt        ; Use Package Equates
  16.  
  17.  
  18. ; ---------- Equates ----------
  19.  
  20. True            Equ        1
  21. False            Equ        0
  22. FNum            Equ        %1111111111000000
  23. FSize            Equ        %0000000000111111
  24.  
  25.  
  26. SetupFontMap
  27.  
  28.     Link            A6,#0
  29.     Movem.l        D0-D7/A0-A4,-(SP)    ; Save the existing register values.
  30.     
  31.     ; Function CountResources (theType: ResType): Integer;
  32.     Clr.w            -(SP)                ; Clear for answer.
  33.     Move.l            #'FONT',-(SP)        ; Count the number of 'FONT' resources.
  34.     _CountResources
  35.     Clr.l            D7
  36.     Move.w        (SP)+,D7            ; Store the result.
  37.     
  38.     ; Create a non-relocatable block into which will be stored each font
  39.     ; resource ID, if the resource has a font size (ie no name).
  40.     Move.l            D7,D0                ; Create a size to allow 2 bytes for
  41.     Lsl.w            #1,D0                ; each resource ID.
  42.     _NewPtr        ,Clear
  43.     Movea.l        A0,A4                ; Store pointer to non-relocatable block.
  44.     
  45.     ; Setup the Resource Manager so that the resources aren't loaded into 
  46.     ; memory.
  47.     ; Procedure SetResLoad (load: Boolean);
  48.     Move.b            #False,-(SP)        ; Load False
  49.     _SetResLoad
  50.     
  51.     Clr.w            D3                    ; Loop counter (# of font resources).
  52.     Clr.w            D4                    ; Offset into the theID non-relocatable block.
  53.     Clr.w            D5                    ; The number of different fonts.
  54.     Clr.l            D6                    ; The # of bytes to allocate to FontMap.
  55.     ; Setup a non-relocatable block for the name of a resource.
  56.     Move.l            #256,D0            ; The # of bytes required.
  57.     _NewPtr        ,Clear
  58.     Move.l            A0,thename(A5)    ; Store pointer.
  59.     
  60. ExamRes            
  61.     Addq.w            #1,D3                ; increment loop counter.
  62.     ; Obtain a handle to a resource with type 'FONT' and an index value from
  63.     ; 1-to-CountResources.
  64.     ; Function GetIndResource (theType: resType; index: Integer): Handle;
  65.     Clr.l            -(SP)                ; Space for handle
  66.     Move.l            #'FONT',-(SP)        ; Font resource type
  67.     Move.w        D3,-(SP)            ; Index value
  68.     _GetIndResource
  69.     
  70.     ; Use the handle to obtain the resources ID and name.
  71.     ; Procedure GetResInfo (theResource: Handle; VAR theID: Integer;
  72.     ;    Var theType: ResType; Var name: Str255);
  73.     ; The handle is already on the stack.
  74.     Pea            theID(A5)            ; point to theID
  75.     Pea            theType(A5)        ; point to theType
  76.     Move.l            thename(A5),-(SP)    ; load the address of the name
  77.     _GetResInfo
  78.     
  79.     Move.w        theID(A5),D0        ; Examine theID to see if the resource
  80.     Andi.w            #FSize,D0            ; has a font size (ie no name).
  81.     Beq.s            NoSize                ; NoSize, ie a name-> NoSize
  82.     ; Place theID of the font resource (which contains a font size) into
  83.     ; the non-relocatable block.  Then increment the offset.
  84.     Move.w        theID(A5),(A4,D4.W)
  85.     Addq.w            #2,D4                ; increment the offset
  86.     Bra.s            Testloop
  87.     
  88. NoSize    
  89.     Movea.l        thename(A5),A0
  90.     ; Evaluate the number of bytes to allocate to this font's name in the
  91.     ; FontMap.  Add this to the running total.
  92.     Clr.l            D0
  93.     Move.b            (A0),D0            ; Create an even length for the name
  94.     Bset.l            #0,D0                ; (ie add padding if its rEquired).
  95.     Addq.l            #1,D0
  96.     Add.l            D0,D6                ; Update the running total.
  97.     Move.l            A0,-(SP)            ;Place the pointer to the name and 
  98.     Move.w        theID(A5),-(SP)    ; the resource ID onto the stack.
  99.     Addq.w            #1,D5                ; Increment the number of different fonts.
  100.     ; Create a new non-relocatable block for the next font resource name.
  101.     Move.l            #256,D0            ; The # of bytes required.
  102.     _NewPtr        ,Clear
  103.     Move.l            A0,thename(A5)    ; Store pointer
  104.  
  105. Testloop
  106.     Cmp.w            D3,D7                ; Have all font resources been
  107.     Bne.s            ExamRes            ; examined, No -> ExamRes
  108.     
  109.     ; Dispose of the pointer in thename(A5) because it is not required.
  110.     Movea.l        thename(A5),A0
  111.     _DisposPtr
  112.     
  113.     ; Setup the Resource Manager so that the resources can be loaded into 
  114.     ; memory.
  115.     ; Procedure SetResLoad (load: Boolean);
  116.     Move.b            #True,-(SP)        ; Load True
  117.     _SetResLoad
  118.     
  119.     ; Evaluate the number of bytes required for the FontMap.  Then create
  120.     ; a relocatable block for the FontMap.  D6 contains the number of bytes
  121.     ; to allocate for the font names in the FontMap.
  122.     Addq.l            #2,D6                ; 2 bytes for "the number of fonts"
  123.     Clr.l            D0
  124.     Move.w        D5,D0                ; The # of different fonts, each font
  125.     Mulu            #6,D0                ; list rEquires 6 bytes.
  126.     Add.l            D0,D6
  127.     Move.l            D0,D3                ; Reserved for later.
  128.     
  129.     ; The amount of space to allocate for the font sizes is 2 x the number
  130.     ; of font resources.  This allows one word per entry.
  131.     Lsl.l            #1,D7
  132.     Add.l            D7,D6                ; Grand Total in D6
  133.     
  134.     Move.l            D6,D0
  135.     _NewHandle    
  136.     Move.l            A0,FontMap(A5)    ; Store the FontMap handle.
  137.     Movea.l        A0,A2                ; Copy the handle.
  138.     ; Lock the FontMap while the font information is placed into it.
  139.     _HLock
  140.     
  141.     Movea.l        (A2),A2            ; Obtain the address of the FontMap.
  142.     Subq.w            #1,D5                ; The number of fonts- 1.
  143.     Move.w        D5,(A2)            ; Store the # of fonts- 1 in the FontMap.
  144.     
  145.     Lea.l            2(A2,D3.W),A1        ; Point to the start of the font name list.
  146.     Lea.l            2(A2),A0            ; Point to the start of the font info list.
  147.     
  148.     ; Transfer theID and the name pointer onto the FontMap from the stack.
  149. Transfer
  150.     Move.w        (SP)+,(A0)+        ; theID
  151.     Move.l            (SP)+,(A0)+        ; name pointer.
  152.     Cmpa.l            A0,A1                ; Have all the resources been transfered?
  153.     Bne.s            Transfer            ; No -> Transfer.
  154.     
  155.     ; The information on the FontMap, in the font info list is now sorted 
  156.     ; so that the first font info has the lowest (alphabetically) name
  157.     ; associated with it.
  158.     Move.w        (A2),D7
  159.     Mulu            #6,D7                ; Point to the last entry in the list
  160. @1    Move.w        D7,D6
  161. @2    Subq.w            #6,D6
  162.     Bmi.s            @3
  163.     Movea.l        2+2(A2,D7.W),A1    ; Load pointer to aStr
  164.     Movea.l        2+2(A2,D6.W),A0    ; Load pointer to bStr
  165.     ; Compare the two strings.
  166.     ; Function IUMagString (aPtr, bPtr: Ptr; aLen, bLen: Integer): Integer;
  167.     Clr.w            -(SP)                ; Clear for result.
  168.     Pea            1(A1)                ; Address of aStr
  169.     Pea            1(A0)                ; Address of bStr
  170.     Clr.w            D0
  171.     Move.b            (A1),D0
  172.     Move.w        D0,-(SP)            ; Length of aStr
  173.     Move.b            (A0),D0
  174.     Move.w        D0,-(SP)            ; Length of bStr
  175.     _IUMagString                        ; Compare aStr and bStr
  176.     Move.w        (SP)+,D0
  177.     Cmpi.w            #1,D0                ; Was aStr greater than bstr
  178.     Beq.s            @2                    ; Yes -> @2.
  179.     
  180.     ; Swap the theID and the name pointers over
  181.     Move.w        2(A2,D7.W),D0        ; Swap theIDs over
  182.     Move.w        2(A2,D6.W),2(A2,D7.W)
  183.     Move.w        D0,2(A2,D6.W)
  184.     Move.l            2+2(A2,D7.W),D0    ; Swap the pointers over
  185.     Move.l            2+2(A2,D6.W),2+2(A2,D7.W)
  186.     Move.l            D0,2+2(A2,D6.W)
  187.     Bra.s            @2
  188.     
  189. @3    Subq.w            #6,D7                ; Shorten the search and repeat until
  190.     Bne.s            @1                    ; the table is completely sorted.
  191.     
  192.     ; Now that the entries in the font info list have been sorted the font 
  193.     ; names are loaded into the FontMap and the name pointer is now 
  194.     ; replaced by a two byte offset,  As each font name is loaded its 
  195.     ; non-relocatable block is disposed of.
  196.     Move.w        (A2),D5
  197.     Addq.w            #1,D5                ; The number of fonts.
  198.     Mulu            #6,D5
  199.     Addq.w            #2,D5
  200.     ; D5 contains the offset to the postion of the first font name.
  201.     
  202.     Clr.w            D7                    ; loop counter (the # of fonts - 1)
  203. LoadNames
  204.     Move.w        D7,D6
  205.     Mulu            #6,D6                ; Point to the next font info
  206.     Movea.l        2+2(A2,D6.W),A3    ; Obtain the pointer to the name.
  207.     Move.w        D5,2+2(A2,D6.W)    ; Store name offset in FontMap.
  208.     
  209.     ; Perform a BlockMove to load the font's name.
  210.     ; Procedure BlockMove (sourcePtr, destPtr: Ptr; byteCount: Size);
  211.     Clr.l            D0
  212.     Move.b            (A3),D0
  213.     Bset.l            #0,D0
  214.     Addq.l            #1,D0                ; # of bytes required to move.
  215.     Lea            (A3),A0            ; load source address
  216.     Lea            (A2,D5.W),A1        ; load destination address
  217.     Add.w            D0,D5                ; point to next name (offset)
  218.     _BlockMove
  219.     Movea.l        A3,A0                ; Dispose of the non-relocatable block
  220.     _DisposPtr                            ; it's no longer required.
  221.     
  222.     Addq.w            #1,D7                ; increment counter
  223.     Cmp.w            (A2),D7
  224.     Ble.s            LoadNames
  225.     
  226.     ; Load the font sizes
  227.     Clr.w            D7                    ; loop counter
  228. LdFSizes
  229.     Lea            (A2,D5.W),A0
  230.     Clr.w            (A0)
  231.     Move.w        D7,D6
  232.     Mulu            #6,D6                ; Point to the next font info
  233.     Move.w        2(A2,D6.W),D3        ; theID of the font
  234.     Move.w        D5,4+2(A2,D6.W)    ; Store size list offset in FontMap
  235.     Clr.w            D2                    ; Offset into the theID block
  236. @1    Move.w        (A4,D2.W),D0        ; Obtain the next theID
  237.     Andi.w            #FNum,D0
  238.     Cmp.w            D3,D0
  239.     Bne.s            @2
  240.     Move.w        (A4,D2.W),D0
  241.     Andi.w            #FSize,D0
  242.     Addq.w            #1,(A0)                ; Increment "# of font sizes" counter
  243.     Move.w        (A0),D1            ; Offset to place font size in list
  244.     Lsl.w            #1,D1                ; Doubled to allow for word length.
  245.     Move.w        D0,(A0,D1.W)        ; Store the font size
  246. @2    Addq.w            #2,D2                ; Increment offset counter
  247.     Cmp.w            D2,D4                ; Examined the complete block
  248.     Bgt.s            @1                    ; No -> @1.
  249.     
  250.     ; Sort the font size list in order of smallest to largest.
  251.     Move.w        (A0),D3            ; The # of font sizes
  252.     Lsl.w            #1,D3                ; Offset to the last font size
  253. Sort1    
  254.     Move.w        D3,D2
  255. Sort2
  256.     Subq.w            #2,D2
  257.     Beq.s            Sort3
  258.     Move.w        (A0,D3.W),D1
  259.     Cmp.w            (A0,D2.W),D1
  260.     Bgt.s            Sort2
  261.     Move.w        (A0,D2.W),(A0,D3.W)
  262.     Move.w        D1,(A0,D2.W)
  263.     Bra.s            Sort2
  264. Sort3
  265.     Subq.w            #2,D3
  266.     Bne.s            Sort1
  267.     
  268.     ; Convert the font "theID" into the font number
  269.     Move.w        2(A2,D6.W),D3
  270.     Lsr.w            #7,D3
  271.     Move.w        D3,2(A2,D6.W)
  272.     
  273.     ; Update the offset in the FontMap (D5) to point to the location of the
  274.     ; font sizes associated with the next font.
  275.     Move.w        (A0),D0
  276.     Addq.w            #1,D0
  277.     Lsl.w            #1,D0
  278.     Add.w            D0,D5
  279.     
  280.     ; Check to see if there are any more fonts to load into the FontMap.
  281.     Addq.w            #1,D7
  282.     Cmp.w            (A2),D7
  283.     Ble.s            LdFSizes
  284.     
  285.     ; The FontMap has been created, and loaded with its information.
  286.     ; Dispose of the theID non-relocatable block and unlock the FontMap.
  287.     Movea.l        A4,A0
  288.     _DisposPtr
  289.     Movea.l        FontMap(A5),A0
  290.     _HUnlock
  291.     
  292.     ; Restore registers to their original values
  293.     Movem.l        (SP)+,D0-D7/A0-A4
  294.     Unlk            A6
  295.     
  296.     Rts
  297.  
  298.     
  299. ; ---------- Variables ----------
  300.  
  301. theID                Ds.w        1    
  302. theType            Ds.l        1
  303. thename            Ds.l        1
  304. FontMap            Ds.l        1            ; Handle of the FontMap.
  305.  
  306. End